//
//  SelecteddController.m
//  ProjectA
//
//  Created by lanouhn on 15/5/30.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "SelecteddController.h"
#import "FirstCenterCell.h"
#import "NetWorkEngine.h"
#import "LoadingTableViewCell.h"
#import "CellController.h"
#import "ONLY.h"

#define kURLSelected @"http://lol.video.luckyamy.com/api/?cat=jieshuo&id=%@&page=%d&ap=lolg&ver=1.1"

@interface SelecteddController ()<NetworkEngineDelegate>
@property (nonatomic ,retain)NSMutableArray *dataSource;
@property (nonatomic ,assign)int PageNumber;
- (void)_startLoadingWithPage:(int)page;
@end

@implementation SelecteddController
- (void)dealloc
{
    [_ID release];
    [_tittle release];
    [_dataSource release];
    [super dealloc];
}
- (NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *a = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 44)]autorelease];
    a.text = self.tittle;
    a.textAlignment = NSTextAlignmentCenter ;
    a.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = a;
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    
    [self.tableView registerClass:[FirstCenterCell class] forCellReuseIdentifier:@"CELL"];
    [self.tableView registerClass:[LoadingTableViewCell class] forCellReuseIdentifier:@"LOAD"];
    self.PageNumber = 1;
    [self _startLoadingWithPage:self.PageNumber];
//    self.tableView.rowHeight = 100;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.refreshControl = [[UIRefreshControl alloc]init];
    [self.refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
    

    
}
- (void)handleRefresh:(UIRefreshControl *)sender{
    self.PageNumber = 1;
    [self _startLoadingWithPage:self.PageNumber];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)_startLoadingWithPage:(int)page{
    NetworkEngine *engine = [NetworkEngine engineWithURL:[NSURL URLWithString:[NSString stringWithFormat:kURLSelected,self.ID,self.PageNumber]] parameters:nil delegate:self];
    [engine start];
//    NSLog(@"%@",[NSString stringWithFormat:kURLSelected,self.ID,self.PageNumber]);
}
- (void)networkDidFinishLoading:(NetworkEngine *)engine withInfo:(id)info{
    [self.refreshControl endRefreshing];
    if (self.PageNumber == 1) {
        [self.dataSource removeAllObjects];
    }
    NSArray *array = [NSJSONSerialization JSONObjectWithData:info options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dic in array) {
        FirstModel *model = [FirstModel modleWithDictionary:dic];
//        NSLog(@"****%@",model);
        [self.dataSource addObject:model];
//        NSLog(@"self.dataSource%@",self.dataSource);
    }
    [self.tableView reloadData];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.dataSource.count + 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataSource.count) {
        return   [tableView dequeueReusableCellWithIdentifier:@"LOAD" forIndexPath:indexPath];
    }
    FirstCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    
    [cell cellWithModel:self.dataSource[indexPath.row] temp:NO];
   
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.dataSource.count) {
        return 30;
    }
    return 110;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CellController *cellVC = [[CellController alloc]init];
    cellVC.model = self.dataSource[indexPath.row];
    [[ONLY creatOnly]passingValueWithContentmodel:self.dataSource[indexPath.row]];
    [self.navigationController pushViewController:cellVC animated:YES];
    [cellVC release];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
